import QtQuick 2.9
import QtQuick.Controls 2.2

import "../common"
import "./curriculum"
import "../../js/util.js" as Util
import "../../js/UI.js" as Style

Rectangle {
    id:userwindow;
    property Component component: null
    property var m_objects: new Array
    signal subpageSignal(var code,var data)
    onSubpageSignal: {
        Util.popView(m_objects)
    }

    Connections{
        target: DatabaseEngine
        onCurriculumChanged:{
            m_list.clear()
            for(var i=0;i<DatabaseEngine.curriculum.length;i++){
                m_list.append({"source":("qrc:/image/curriculum/%1.jpg").arg(DatabaseEngine.curriculum[i].cur_id),
                                  "name":DatabaseEngine.curriculum[i].cur_name,
                                  "university": DatabaseEngine.curriculum[i].cur_school,
                                  "week": DatabaseEngine.curriculum[i].cur_time,
                                  "numbers": DatabaseEngine.curriculum[i].cur_numbers,
                                  "cur_id":DatabaseEngine.curriculum[i].cur_id
                              })
            }
        }
    }

    //图片地址数组↓
    property var imagelist: ["qrc:/image/curriculum/lunbo0.jpg"
        ,"qrc:/image/curriculum/m_lunbo1.jpg"
        ,"qrc:/image/curriculum/lunbo2.jpg"];
    property var i: 0;

    //图片组件
    Component{
        id:swipeImage;
        Image{
            asynchronous: true;
        }
    }

    Item{
        id: m_title
        width: parent.width
        height: m_lineValue*1.4
        anchors.top: parent.top
        AText{
            anchors.left: parent.left
            anchors.leftMargin: 0.3*m_lineValue
            anchors.verticalCenter: parent.verticalCenter
            color: "#000000"
            font.pointSize: 23
            text: qsTr("精品课程")
        }
        BButton{
            anchors.right: parent.right
            anchors.rightMargin: 0.2*m_lineValue
            anchors.verticalCenter: parent.verticalCenter
            enabled: true
            m_text.text: "我的课程"
            m_text.font.pointSize: 14
            onSigClick: {
                DatabaseEngine.setMyCurriculum()
                Util.pushView(component,m_objects,"qrc:/qml/form/curriculum/MyCurriculum.qml",m_root_id,{})
            }
        }

        SplitLine {
            anchors {
                bottom: parent.bottom
                left: parent.left
                right: parent.right;
            }
        }
    }

    Flickable{
        id: flick
        anchors{
            top: m_title.bottom
            left: parent.left
            right: parent.right
        }
        width: parent.width
        height: parent.height - m_title.height
        contentHeight: item.childrenRect.height
        clip: true
        Rectangle {
            id: item
            width: parent.width
            height: childrenRect.height
            color: "#f6f5ec"
            Rectangle{
                id:rect
                width: parent.width-m_lineValue*0.4
                height: m_lineValue*3
                anchors.top: parent.top
                anchors.topMargin: 10
                anchors.horizontalCenter: parent.horizontalCenter
                clip: true	//截断多出来的部分
                radius: 20
                SwipeView{
                    id:swipeview;
                    anchors.fill: parent;

                    Timer{//3.5秒后切换图片
                        id:imageSwitch;
                        interval: 3500;
                        repeat: true;
                        onTriggered: {
                            swipeview.currentIndex=(swipeview.currentIndex+1)%3;
                            indicator.currentIndex=swipeview.currentIndex;
                        }
                    }
                }

                //如果鼠标在轮播图里就停止，方便浏览
                MouseArea{
                    anchors.fill: parent;
                    hoverEnabled: true;
                    onEntered: {
                        imageSwitch.stop();
                    }
                    onExited: {
                        imageSwitch.start();
                    }
                }

                PageIndicator{
                    id:indicator;
                    count:imagelist.length;
                    interactive: true;//可点击
                    anchors.bottom: rect.bottom;
                    anchors.bottomMargin: 4;
                    anchors.horizontalCenter: rect.horizontalCenter;

                    onCurrentIndexChanged: {
                        swipeview.currentIndex=currentIndex;
                    }
                }
            }

            Rectangle{
                id: m_body
                width: parent.width
                height: childrenRect.height
                anchors.top: rect.bottom
                anchors.topMargin: 10
                color: "transparent"
                ListView{
                    id: listview
                    width: parent.width
                    height: childrenRect.height
                    anchors.top: parent.top
                    anchors.topMargin: m_lineValue*0.2
                    clip: true
                    model: m_list
                    spacing:10
                    delegate: Curriculumdelegate{
                        m_source: model.source
                        m_name: model.name
                        m_university: model.university
                        m_week: model.week
                        m_numbers: model.numbers
                        onClickedsignal: {
                            Util.pushView(component,m_objects,"qrc:/qml/form/curriculum/Selectcurriculum.qml",m_root_id,{"currentId":model.cur_id})
                        }
                    }
                }
            }
        }
    }

    ListModel{
        id: m_list
    }

    //根据图片数组的数量来决定生成组件的数量
    Component.onCompleted: {
        for(i;i<imagelist.length;i++)
        {
            swipeImage.createObject(swipeview,{"source":imagelist[i],"x":swipeview.x,"y":swipeview.y,
                                        "width":swipeview.width,"height":swipeview.height});
        }
        swipeview.currentIndex=0;
        imageSwitch.start();

        m_list.clear()
        for(var j=0;j<DatabaseEngine.curriculum.length;j++){
            m_list.append({"source":("qrc:/image/curriculum/%1.jpg").arg(DatabaseEngine.curriculum[j].cur_id),
                              "name":DatabaseEngine.curriculum[j].cur_name,
                              "university": DatabaseEngine.curriculum[j].cur_school,
                              "week": DatabaseEngine.curriculum[j].cur_time,
                              "numbers": DatabaseEngine.curriculum[j].cur_numbers,
                              "cur_id":DatabaseEngine.curriculum[j].cur_id
                          })
        }
    }
}
